package com.csx.midware.redis;

import redis.clients.jedis.Jedis;

/**
 * Redis 有序集合的使用
 */
public class RedisRankingExample {

    private Jedis jedis;

    public RedisRankingExample() {
        jedis = new Jedis("localhost");
    }

    public void addScore(String rankingKey, String member, double score) {
        jedis.zadd(rankingKey, score, member);
    }

    public void getTopRankings(String rankingKey, int count) {
        jedis.zrevrangeWithScores(rankingKey, 0, count - 1).forEach(entry -> {
            System.out.println("Member: " + entry.getElement() + ", Score: " + entry.getScore());
        });
    }

    public static void main(String[] args) {
        RedisRankingExample example = new RedisRankingExample();
        example.addScore("leaderboard", "Alice", 100);
        example.addScore("leaderboard", "Bob", 95);
        example.addScore("leaderboard", "Charlie", 90);

        System.out.println("Top 2 Rankings:");
        example.getTopRankings("leaderboard", 2);
    }
}